﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using CellarDoor.Gui;

namespace CellarDoor.Gui.Theming
{
    [System.Diagnostics.DebuggerDisplay("Name = {Name}; Resolution = {Resolution}")]
    public class UiThemeElement : INamedWithResolution
    {
        public string Name { get; private set; }
        public int Resolution { get; private set; }
        private IEnumerable<UiThemeSprite> states;

        public UiThemeElement(string name, int resolution, IEnumerable<UiThemeSprite> states)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (states == null)
            {
                throw new ArgumentNullException("states");
            }
            this.Name = name;
            this.Resolution = resolution;
            this.states = states;
        }

        public bool TryGetState(string name, out UiThemeSprite state)
        {
            state = null;
            foreach (UiThemeSprite item in states)
            {
                if (String.Compare(name, item.Name, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    state = item;
                }
            }

            if (state == null)
            {
                state = default(UiThemeSprite);
                return false;
            }

            return true;
        }

        public UiThemeSprite GetState(string name)
        {
            UiThemeSprite result = null;

            foreach (UiThemeSprite item in states)
            {
                // the first items in the collection is the default element definition
                if (result == null)
                {
                    result = item;
                }
                if (String.Compare(name, item.Name, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    result = item;
                }
            }

            return result;
        }
    }
}
